package com.xm.common.entity;

import com.alibaba.fastjson2.JSONObject;
import lombok.Data;

@Data
public class Response {
    int id;
    int code;
    Object data;
    String message;

    public Response(int i, int i1, Object o, String message) {
        this.id = i;
        this.code = i1;
        this.data = o;
        this.message = message;
    }

    public boolean success() {
        return code == 200;
    }

    public JSONObject asJson() {
        return JSONObject.from(data);
    }

    public String asString() {
        return data.toString();
    }

    public static Response errorResponse(Exception e) {
        return new Response(0, 500, null, e.getMessage());
    }
}
